package cn.gjculture.shopping.api.service;

import cn.gjculture.shopping.api.dao.TokenDao;
import cn.gjculture.shopping.api.entity.Order;
import cn.gjculture.shopping.api.entity.ParamData;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.entity.Token;
import cn.gjculture.shopping.api.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class TokenService {

    Map<String, Token> tokenMap = new ConcurrentHashMap<>();

    @Autowired
    TokenDao tokenDao;

    public Token getToken(String id) {
        Token token = new Token(true);
        token.setCustomerId(id);
        this.deleteToken(id);
        token.setExpirationTime(this.getExpirationTime());
        tokenMap.put(token.getTokenId(), token);
        return token;
    }

    public void deleteToken(String id) {
        Token token=this.query(id);
        if(token!=null){
            tokenMap.remove(token.getTokenId());
        }
    }

    public void updateExpirationTime(String token) {
        Token t = tokenMap.get(token);
        t.setExpirationTime(getExpirationTime());
    }

    private long getExpirationTime() {
        long expiration = 30 * 60 * 1000;
        return System.currentTimeMillis() + expiration;
    }


    public boolean verifySign(String jsonString, Order order, ParamData param, String sign) {
        Return ret = new Return();
        Token t = this.query(order.getCustomerId());
        if (t.getTokenId().equals(param.getCommon().getToken())) {
            StringBuilder beforeData = new StringBuilder(t.getTokenId());
            beforeData.append(jsonString);
            beforeData.append(t.getPrivateKey());
            beforeData.append(order.getCustomerId());
            beforeData.append(param.getCommon().getTimestamp());
            if (t.getExpirationTime() > System.currentTimeMillis()) {
                if (UUIDUtil.encrypt(beforeData.toString()).equals(sign)) {
                    return true;
                }
            } else {
                ret.getCommon().setError("9999");
                ret.getCommon().setMessage("token过期，请重新登录");
            }
        }
        return false;
    }

    public Token query(String customerId) {
        for (Map.Entry<String, Token> tokenEntry : tokenMap.entrySet()) {
            Token token=tokenEntry.getValue();
            if (token.getCustomerId().equals(customerId)) {
                if(token.isNoLate()){
                    return token;
                }else{
                    this.clearLate();
                    return null;
                }
            }
        }
        return null;
    }

    private void clearLate(){
        for (Map.Entry<String, Token> tokenEntry : tokenMap.entrySet()) {
            if (!tokenEntry.getValue().isNoLate()) {
                tokenMap.remove(tokenEntry.getKey());
            }
        }
    }

    public Token queryByToken(String token) {
        Token token1=tokenMap.get(token);
        if(token1!=null&&token1.isNoLate()){
            return tokenMap.get(token);
        }else{
            this.clearLate();
        }
        return null;
    }
}
